Skip to content

Latest commit

 

History

History
41 lines (33 loc) · 999 Bytes

File metadata and controls

41 lines (33 loc) · 999 Bytes

92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in one-pass.

Note: 1 ≤ mn ≤ length of list.

Example:

Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5->NULL 

Solutions (Python)

1. Solution

# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclassSolution: defreverseBetween(self, head: ListNode, m: int, n: int) ->ListNode: dummy=ListNode(next=head) start=curr=dummynext=curr.nextfor_inrange(m-1): start=curr=nextnext=curr.nextfor_inrange(n-m+1): prev=currcurr=nextnext=curr.nextcurr.next=prevstart.next.next=nextstart.next=currreturndummy.next
close